package edu.bath.sensorvisualiser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import edu.bath.sensorframework.Visualisation;

public class Face implements Visualisation<Float> {
	private Float data = 0.0f;
	private Canvas c;
	private Map<Float, Bitmap> images = Collections.synchronizedMap(new HashMap<Float, Bitmap>(3));
	private int bgCol;
	private Bitmap defaultImage = null;
	
	public Face(Canvas c, int bgCol, Bitmap defaultImage) {
		this.c = c;
		this.bgCol = bgCol;
		this.defaultImage = defaultImage;
	}
	
	public Map<Float, Bitmap> getImageMap() { return this.images; }
	
	/**
	 * if a change is needed, return the Bitmap to display.
	 */
	private Bitmap check() throws IllegalArgumentException {
		List<Float> dataList = new ArrayList<Float>(images.size());
		dataList.addAll(images.keySet());
		Float lowest = null;
		
		synchronized(images) {
			for(int n = 0 ; n < dataList.size() ; n++) {
				if(data < dataList.get(n)) {
					if(lowest == null || dataList.get(n) < lowest)
						lowest = dataList.get(n);
				}
			}
		}
		
		if(lowest == null)
			return defaultImage;
		return images.get(lowest);
	}
	
	public void setDefaultImage(Bitmap image) {
		this.defaultImage = image;
	}
	
	@Override
	public void draw() {
		c.drawColor(this.bgCol);
		c.drawBitmap(check(), 0, 0, null);
	}

	@Override
	public void addData(Float data) {
		this.data = data;
	}

	@Override
	public void flushData() {
		this.data = 0.0f;
	}
}
